package org.dromara.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.bean.BeanUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.binding.Binder;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.dromara.common.core.exception.BusiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysCommonFieldsBo;
import org.dromara.system.domain.vo.SysCommonFieldsVo;
import org.dromara.system.domain.SysCommonFields;
import org.dromara.system.mapper.SysCommonFieldsMapper;
import org.dromara.system.service.ISysCommonFieldsService;
import org.springframework.transaction.annotation.Transactional;
import org.dromara.common.mybatis.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.function.Function;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 常用字段Service业务层处理
 *
 * @author yilong.fu
 * @date 2024-11-24
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class SysCommonFieldsServiceImpl extends ServiceImpl<SysCommonFieldsMapper, SysCommonFields, SysCommonFieldsBo, SysCommonFieldsVo> implements ISysCommonFieldsService {


    public SysCommonFieldsVo fill(SysCommonFieldsVo entity) {
        SysCommonFieldsVo vo = Binder.convertAndBindRelations(entity, SysCommonFieldsVo.class);
        BeanUtil.copyProperties(vo, entity);
        return vo;
    }

    public LambdaQueryWrapper<SysCommonFields> buildQueryWrapper(LambdaQueryWrapper<SysCommonFields> lqw,SysCommonFieldsBo bo) {
        lqw.like(StringUtils.isNotBlank(bo.getFieldName()), SysCommonFields::getFieldName, bo.getFieldName());
        lqw.eq(StringUtils.isNotBlank(bo.getField()), SysCommonFields::getField, bo.getField());
        lqw.eq(StringUtils.isNotBlank(bo.getDataType()), SysCommonFields::getDataType, bo.getDataType());
        lqw.eq(StringUtils.isNotBlank(bo.getDataLength()), SysCommonFields::getDataLength, bo.getDataLength());
        lqw.eq(bo.getAllowNull() != null, SysCommonFields::getAllowNull, bo.getAllowNull());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SysCommonFields::getDescription, bo.getDescription());
        lqw.eq(bo.getSeq() != null, SysCommonFields::getSeq, bo.getSeq());
        return lqw;
    }


    /**
     * 更新前的数据校验
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean validEntityBeforeUpdate(SysCommonFields entity) {
        //TODO 做一些数据校验,如唯一约束
        return true;
    }

    /**
     * 保存前的数据校验
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean validEntityBeforeSave(SysCommonFields entity) {
        //TODO 做一些数据校验,如唯一约束
        return true;
    }

    @Override
    public List<SysCommonFieldsVo> importList(List<SysCommonFieldsVo> datas, Integer mode, boolean updatePart) {
        if (CollUtil.isEmpty(datas)) {
            throw new BusiException("导入数据不能为空");
        }
        List<SysCommonFieldsVo> errorList = new ArrayList<>();
        datas.forEach(item -> {
            SysCommonFieldsBo sysCommonFieldsBo = new SysCommonFieldsBo();
            BeanUtil.copyProperties(item, sysCommonFieldsBo);
            if (mode == 1) {   //新增和修改
                if (sysCommonFieldsBo.getField() != null) {
                    SysCommonFieldsVo exists = getOne(new SysCommonFieldsBo().builder().field(sysCommonFieldsBo.getField()).build());
                    if (exists == null) {
                        insertByBo(sysCommonFieldsBo);
                    } else {
                        sysCommonFieldsBo.setId(exists.getId());
                        updateByBo(sysCommonFieldsBo);
                    }
                } else {

                    insertByBo(sysCommonFieldsBo);
                }
            } else if (mode == 2) {
                if (sysCommonFieldsBo.getField() != null) {
                    SysCommonFieldsVo exists = getOne(new SysCommonFieldsBo().builder().field(sysCommonFieldsBo.getField()).build());
                    if (exists != null) {
                    } else {
                        insertByBo(sysCommonFieldsBo);
                    }
                } else {
                    insertByBo(sysCommonFieldsBo);
                }
            } else {
                if (sysCommonFieldsBo.getField() != null) {
                    SysCommonFieldsVo exists = getOne(new SysCommonFieldsBo().builder().field(sysCommonFieldsBo.getField()).build());

                    if (exists != null) {
                        sysCommonFieldsBo.setId(exists.getId());
                        updateByBo(sysCommonFieldsBo);
                    }
                }
            }
        });
        return errorList;
    }
}
